package com.library.servlet;

import com.library.util.DBUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import org.json.JSONArray;

@WebServlet("/books")
public class BookListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        
        String searchTerm = request.getParameter("q");
        String sql = "SELECT id, title, author, isbn, publisher, publish_date, category, total_copies, available_copies, status, image_path FROM books WHERE status = 'active' ";

        if (searchTerm != null && !searchTerm.trim().isEmpty()) {
            sql += " AND (title LIKE ? OR author LIKE ? OR isbn LIKE ? OR publisher LIKE ? OR category LIKE ?)";
        }
        sql += " ORDER BY create_time DESC";
        
        List<JSONObject> books = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection()) {
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                int paramIndex = 1;
                if (searchTerm != null && !searchTerm.trim().isEmpty()) {
                    String likeTerm = "%" + searchTerm + "%";
                    pstmt.setString(paramIndex++, likeTerm);
                    pstmt.setString(paramIndex++, likeTerm);
                    pstmt.setString(paramIndex++, likeTerm);
                    pstmt.setString(paramIndex++, likeTerm);
                    pstmt.setString(paramIndex++, likeTerm);
                }

                try (ResultSet rs = pstmt.executeQuery()) {
                
                while (rs.next()) {
                    JSONObject book = new JSONObject();
                    book.put("id", rs.getInt("id"));
                    book.put("title", rs.getString("title"));
                    book.put("author", rs.getString("author"));
                    book.put("isbn", rs.getString("isbn"));
                    book.put("publisher", rs.getString("publisher"));
                    book.put("publish_date", rs.getString("publish_date"));
                    book.put("category", rs.getString("category"));
                    book.put("total_copies", rs.getInt("total_copies"));
                    book.put("available_copies", rs.getInt("available_copies"));
                    book.put("status", rs.getString("status"));
                    book.put("image_path", rs.getString("image_path"));
                    books.add(book);
                }
            }
        }
        } catch (SQLException e) {
            e.printStackTrace();
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write(new JSONObject().put("error", "数据库查询失败").toString());
            return;
        }

        response.getWriter().write(new JSONArray(books).toString());
    }
} 